import QtQuick 2.12
import QtQuick.Controls 2.5
import boan.qt.FileSaver 1.0

Page {
    width: 600
    height: 400

    property var arrMs: []

    DebugBtns {
        id: dbtn
        anchors.left: parent.left
        anchors.top: parent.top
        anchors.leftMargin: 5
        anchors.topMargin: 200

        onSigDebugStarted: {
            setFeedState(0, 2, "feeding")
        }

        onSigDebugPaused: {
            setFeedState(0, 2, "fed")
        }

        onSigDebugStopped: {
            setFeedState(0, 3, "notfed")
        }
    }

    ListView {
        id: cxx
        spacing: 20
        anchors.fill: parent
        anchors.leftMargin: 150
        anchors.rightMargin: 5

        delegate: LineZhuLan {
            lineNum: model.index+1
            arrModel: model.ms
            showIndicator: true
        }

        model: ListModel {
            id: lmodel
//            ListElement{lineNum:1}
        }

    }

    FileSaver {
        id: fs
    }


    Component.onCompleted: {
        arrMs = fs.getFeedInfo()
        arrMs.forEach(i => {
                          for (var ind in i) {
                              i[ind].feedState = "notfed"
                          }
                          lmodel.append(i)
                      })
    }

    function reloadFeedInfo() {
        arrMs = fs.getFeedInfo()
        lmodel.clear()
        arrMs.forEach(i => {lmodel.append(i)})
    }

    function setFeedState(lineIndex, zhulanIndex, sta) {
        arrMs[lineIndex].ms[zhulanIndex].feedState = sta
//        print(JSON.stringify(arrMs))
//        lmodel.clear()
//        arrMs.forEach(i => {lmodel.append(i)})
        fs.write(arrMs)
        reloadFeedInfo()
    }

}
